package com.xyh;

import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;

@Configuration
public class MyConfig {
    @MyLoadBalanced
    @Autowired(required = false)
    private List<RestTemplate> tpls= Collections.emptyList();
    @Bean
    public SmartInitializingSingleton lbInitializing(){
        return new SmartInitializingSingleton() {
            public void afterSingletonsInstantiated() {
                for (RestTemplate tpl:tpls) {
                  List<ClientHttpRequestInterceptor> interceptors  =tpl.getInterceptors();
                   interceptors.add(new MyInterceptor());
                   tpl.setInterceptors(interceptors);
                }
            }
        };
    }
}
